package com.handinfo.android.ui.window;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;

import android.graphics.Bitmap;

import com.handinfo.android.DWGameManager;
import com.handinfo.android.core.net.GameProtocol;
import com.handinfo.android.game.Cell;
import com.handinfo.android.game.IconManger;
import com.handinfo.android.game.Role;
import com.handinfo.android.game.item.Item;
import com.handinfo.android.game.item.ItemConstants;
import com.handinfo.android.game.item.ItemEquipment;
import com.handinfo.android.game.item.SellItem;
import com.handinfo.android.ui.IUIWindows;
import com.handinfo.android.ui.UIWindows;
import com.handinfo.android.uicontrols.DWControlsManager;
import com.handinfo.android.uicontrols.DWDragListener;
import com.handinfo.android.uicontrols.DWListener;
import com.handinfo.android.uicontrols.DWLongListener;
import com.handinfo.android.uicontrols.controls.DWBackground;
import com.handinfo.android.uicontrols.controls.DWButton;
import com.handinfo.android.uicontrols.controls.DWControl;
import com.handinfo.android.uicontrols.controls.DWFrame;
import com.handinfo.android.uicontrols.controls.DWGrid;
import com.handinfo.android.uicontrols.controls.DWGrids;
import com.handinfo.android.uicontrols.controls.DWImageBox;
import com.handinfo.android.uicontrols.controls.DWInputBox;
import com.handinfo.android.uicontrols.controls.DWLabel;
import com.handinfo.android.uicontrols.controls.DWListSubItem;
import com.handinfo.android.uicontrols.controls.DWListbox;
import com.handinfo.android.uicontrols.controls.DWMessageBox;
import com.handinfo.android.uicontrols.controls.DWTextbox;
import com.handinfo.android.uicontrols.controls.DWTitle;
import com.handinfo.android.utils.Tools;

/**
 * 摆摊系统
 * 
 * @author kenny
 * 
 */
public class UIBaitan implements IUIWindows {

	private static final byte[] BAG_TYPE = { Cell.TYPE_COLUMN_ROLEBAG,
			Cell.TYPE_COLUMN_SHOP };
	private static final String[] BUTTONMSG = { "背包", "摆摊" };
	private DWFrame m_Interrupt_Frame;
	private DWFrame m_warehose;
	private DWTitle[] m_title = new DWTitle[2];
	private DWImageBox m_close;
	private DWBackground[] m_background = new DWBackground[2];
	private DWGrids[] m_grids = new DWGrids[2];
	private DWButton[] m_button = new DWButton[2];
	private DWGrid selectControl;

	private Bitmap img_close;
	private Bitmap img_lock;
	private Bitmap img_cell;
	private Bitmap img_button;
	private Bitmap img_anxia;
	private Bitmap img_itemButton;
	private Bitmap img_itemAnxia;
	private Bitmap img_background;
	private Bitmap img_bagTitle;
	private Bitmap img_warehoseTitle;

	byte nowsize = 10; // 当前摆摊栏格子数量
	byte maxsize = 20; // 摆摊栏总格子数量

	public boolean SELL_TYPE = false;

	private Cell[][] m_column = new Cell[2][];

	public void testAnnouncement() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		;
		try {
			dos.writeByte(1);
			dos.writeByte(10);
			dos.writeByte(10);
			dos.writeByte(0);

		} catch (IOException e) {
		}
		ByteArrayInputStream das = new ByteArrayInputStream(baos.toByteArray());
		DataInputStream dis = new DataInputStream(das);

		try {
			DWGameManager.getInstance().m_role.recvColumnShop(dis);
		} catch (Exception e) {

		}
	}

	public void init() {

		testAnnouncement();

		img_close = UIWindows.createImage("/img/newui/fanhuianniu_1.gnp");
		img_lock = UIWindows.createImage("/img/newui/beibaost_1.gnp");
		img_cell = UIWindows.createImage("/img/newui/wupinlan.gnp");
		img_button = UIWindows.createImage("/img/newui/anniu_1.gnp");
		img_anxia = UIWindows.createImage("/img/newui/anniu_1ax.gnp");
		img_background = UIWindows.createImage("/img/newui/beibaobj_2.gnp");
		img_bagTitle = UIWindows.createImage("/img/newui/beibao_1.gnp");
		img_warehoseTitle = UIWindows.createImage("/img/newui/cangku_1.gnp");
		img_itemButton = UIWindows.createImage("/img/newui/anniu_2.gnp");
		img_itemAnxia = UIWindows.createImage("/img/newui/anniu_2ax.gnp");
	}

	private void initFrame() {
		if (m_warehose != null
				&& DWControlsManager.getInstance().contains(m_warehose)) {
			DWControlsManager.getInstance().removeControl(m_warehose);
			m_warehose = null;
			// 刷新界面
		}
		m_warehose = new DWFrame(DWFrame.FRAME_TYPE_FULL);
		m_warehose.setClickClose(false);

		m_title[0] = new DWTitle("背包", m_warehose);
		m_title[0].setShowWideHigh(m_warehose.getShowWidth() / 2 + 8,
				m_title[0].getShowHeight());
		m_title[0].setNearAnchor(m_warehose, Tools.RIGHT_BOTTOM,
				Tools.RIGHT_TOP, 8, 6);
		m_title[0].setBackground(img_bagTitle, false);
		m_warehose.addControl(m_title[0]);

		m_title[1] = new DWTitle(m_name, m_warehose);
		m_title[1].setShowWideHigh(m_warehose.getShowWidth() / 2 + 8,
				m_title[1].getShowHeight());
		// m_title[1].setBackground(img_warehoseTitle, false);
		m_title[1].setNearAnchor(m_warehose, Tools.LEFT_BOTTOM, Tools.LEFT_TOP,
				-8, 6);
		m_warehose.addControl(m_title[1]);

		m_close = new DWImageBox(img_close);
		m_close.setNearAnchor(m_title[0], Tools.RIGHT_VCENTER,
				Tools.RIGHT_VCENTER);
		m_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		m_close.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item = null;
				}
				m_name = DWGameManager.getInstance().m_role.m_name + "的摊位";
				close((byte) 0);
			}
		});
		m_warehose.addControl(m_close);

		for (int i = 0; i < 2; i++) {
			m_background[i] = new DWBackground(
					(m_warehose.getShowWidth() - 80) / 2,
					m_warehose.getShowHeight() - 55 - img_button.getHeight());
			m_background[i].setNearAnchor(m_warehose,
					(i == 0 ? Tools.LEFT_VCENTER : Tools.RIGHT_VCENTER),
					Tools.HCENTER_VCENTER, (i == 0 ? 20 : -20),
					-img_button.getHeight() / 4);
			m_warehose.addControl(m_background[i]);
		}

		int gridsX = (m_background[0].getShowWidth() - img_cell.getWidth() * 5 - 30) / 5;
		int gridsY = (m_background[0].getShowHeight() - img_cell.getWidth() * 4 - 40) / 4;

		for (int i = 0; i < 2; i++) {
			final int index = i;
			m_grids[i] = new DWGrids(img_cell,
					Role.COLUMN_MAX_SIZE[BAG_TYPE[i]], 5, 4);
			m_grids[i].setInterspace(gridsX, gridsY);
			m_grids[i].setNearAnchor(m_background[i], Tools.HCENTER_VCENTER,
					Tools.HCENTER_VCENTER);
			setGrids(i, m_column[i]);
			addGridListener(m_grids[i], index);
			m_warehose.addControl(m_grids[i]);
			if (SELL_TYPE) {
				BUTTONMSG[1] = "收摊";
			} else {
				BUTTONMSG[1] = "摆摊";
			}
			m_button[i] = new DWButton(BUTTONMSG[i], img_button);
			m_button[i].setDownImage(img_anxia);
			m_button[i].addListener(new DWListener() {
				public void OnDoubleClick() {
				}

				public void OnClick() {
					if (index == 0) {
						m_button[index].setFailureTime(30);
						Tools.debugPrintln("整理背包 -> actionTrim");
						DWGameManager.getInstance().getSendMessage()
								.sendArrangeMentColumnCommand(BAG_TYPE[index]);
					} else {
						if (SELL_TYPE) {
							// SELL_TYPE = false;
							// BUTTONMSG[index] = "摆摊";
							// m_button[index].setName(BUTTONMSG[index]);啊啊啊

							sendBaitansellend();

						} else {
							int num = 0;
							for (Cell c : DWGameManager.getInstance().m_role.m_column_shop) {
								if (c.m_item != null) {
									num++;
								}
							}
							if (num < 1) {
								DWGameManager.getInstance().addSystemInfo(
										DWGameManager.SYSTEM_INFO_TYPE_B,
										"请放置贩卖物品！");
							} else {
								drawtanweiname();
							}
						}

					}
				}
			});
			m_button[i].setNearAnchor(m_background[i], i == 0 ? Tools.RIGHT_TOP
					: Tools.LEFT_TOP, i == 0 ? Tools.RIGHT_BOTTOM
					: Tools.LEFT_BOTTOM, 0, 10);
			m_warehose.addControl(m_button[i]);
		}

	}

	private void initDate() {
		if (DWGameManager.getInstance().m_role != null) {
			m_column[0] = DWGameManager.getInstance().m_role.m_column_bag;
			m_column[1] = DWGameManager.getInstance().m_role.m_column_shop;

		}
	}

	/**
	 * 添加格子中的内容
	 */

	private void setGrids(final int num, final Cell[] cell) {
		if (cell == null)
			return;

		if (DWGameManager.getInstance().m_role != null) {

			m_grids[num].setGridsImage(img_cell, 0, cell.length - 1);
			m_grids[num].setGridsImage(img_lock, cell.length,
					Role.COLUMN_MAX_SIZE[BAG_TYPE[num]] - 1);

			Tools.debugPrintln("cell.length:" + cell.length);
			Tools.debugPrintln("cell.length:"
					+ Role.COLUMN_MAX_SIZE[BAG_TYPE[num]]);

			for (int i = 0; i < cell.length; i++) {
				m_grids[num].m_grids.get(i).m_data = cell[i];
			}
		}
	}

	/**
	 * 添加格子监听
	 */
	private DWMessageBox msg;

	private void addGridListener(final DWGrids grids, final int type) {
		grids.addListener(new DWListener() {

			public void OnDoubleClick() {
			}

			public void OnClick() {
				DWGrid flaggrid = grids.getTouchGrid();

				if (selectControl != null && (!flaggrid.equals(selectControl))) {
					selectControl.setSelect(false);
				}
				selectControl = flaggrid;

				DWGrid grid = grids.getTouchGrid();
				if (grid.m_data != null) {
					if (type == 0) {
						if (((Cell) grid.m_data).m_item != null) {
							showItemMessage((Cell) grid.m_data, UIWindows
									.getInstance().m_bag.getButtonMenuList(
									UIBag.BAG_STATE_NORMAL, (Cell) grid.m_data));
						}
					} else {
						showItemMessage((Cell) grid.m_data, null);
					}
				} else {
					int openNum = grids.getTouchIndex() + 1
							- m_column[type].length;
					Role role = DWGameManager.getInstance().m_role;
					long price = (type == 0 ? role.m_bag_unitPrice
							: role.m_warehouse_unitPrice / 10000);

					msg = new DWMessageBox("提示", "你确定花费" + (openNum * price)
							+ (type == 0 ? "元宝" : "万金币") + "开启" + openNum + "个"
							+ BUTTONMSG[type] + "格子");
					msg.addListener(new DWListener() {
						public void OnDoubleClick() {
						}

						public void OnClick() {
							DWControlsManager.getInstance().removeControl(msg);
							if (msg.getResult() == DWMessageBox.SOFTKEY_OK) {
								actionKuorong(BAG_TYPE[type],
										grids.getTouchIndex());
							}
						}
					});
					DWControlsManager.getInstance().addControl(msg);
				}
			}
		});
		grids.addDragListener(new DWDragListener() {

			public void OnDrag(DWControl control) {
				if (control instanceof DWGrids) {
					DWGrids in_grids = (DWGrids) control;
					if (grids.equals(in_grids)) {
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "不能进行移动操作");
					} else {
						// Cell fromCell = (Cell)
						// (in_grids.getTouchGrid().m_data);
						// Cell endCell = (Cell) (grids.getDragGird().m_data);
						// actionMove(fromCell, endCell);// 移动格子中的物品
						if (grids.getDragGird().m_data == null) {
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B,
									"不能进行移动操作");
						} else {

							Cell fromCell = (Cell) (in_grids.getTouchGrid().m_data);
							Cell endCell = (Cell) (grids.getDragGird().m_data);
							Tools.debugPrintln("进入拖拽");
							if (fromCell.m_item.m_bindingState == ItemConstants.绑定状态_否
									&& fromCell.m_item
											.isOperate(ItemConstants.可交易)) {
								if (SELL_TYPE) {
									DWGameManager.getInstance().addSystemInfo(
											DWGameManager.SYSTEM_INFO_TYPE_B,
											"请收摊后再进行移动操作！");
								} else {
									actionMove(fromCell, endCell);
								}
							} else {
								DWGameManager.getInstance().addSystemInfo(
										DWGameManager.SYSTEM_INFO_TYPE_B,
										"绑定道具不可添加");

							}
						}
					}
				}
			}
		});

		grids.addLongListener(new DWLongListener() {

			public void OnLong(boolean isDown) {
				DWGrid flaggrid = grids.getTouchGrid();

				if (selectControl != null && (!flaggrid.equals(selectControl))) {
					selectControl.setSelect(false);
				}
				selectControl = flaggrid;
				selectControl = grids.getTouchGrid();
			}
		});
	}

	/**
	 * 显示物品详情及功能
	 */
	private DWFrame showFrame;
	private boolean m_selljiage = false;

	private void showItemMessage(final Cell cell, final String[] buttonMenu) {
		if (cell == null || cell.m_item == null) {
			m_selljiage = false;
			return;
		}

		if (showFrame != null
				&& DWControlsManager.getInstance().contains(showFrame)) {
			DWControlsManager.getInstance().removeControl(showFrame);
			showFrame = null;
		}

		if (cell.m_column_type == Cell.TYPE_COLUMN_SHOP) {
			m_selljiage = true;
		} else {
			m_selljiage = false;
		}

		Cell m_bag_cell_equip = DWGameManager.getInstance().m_role.m_column_equip[cell.m_item.m_subtype];
		int showWidth = ((DWGameManager.Screen_Width * 5) >> 4) + 50;

		showFrame = new DWFrame(showWidth, showWidth);

		showFrame.setTouchZoomIn(0, 0);
		showFrame.setClickClose(true);
		showFrame.setBackgroundColor(Tools.ALPHA);

		// 显示出售价格
		if (m_selljiage) {
			DWTextbox tb = new DWTextbox(
					"<mark c=FFC004>"
							+ "出售价格："
							+ (cell.m_item.m_sell.m_UntiPrice * cell.m_item.m_sell.m_num)
							+ "</mark>");
			tb.setNearAnchor(m_background[1], Tools.LEFT_TOP, Tools.LEFT_TOP,
					0, m_background[0].getShowHeight() - tb.getShowHeight());
			showFrame.addControl(tb);
		}

		DWBackground itemBackground = new DWBackground(img_background,
				showFrame.getShowWidth(), showFrame.getShowHeight());
		itemBackground.setNearAnchor(showFrame, Tools.HCENTER_VCENTER,
				Tools.HCENTER_VCENTER);
		showFrame.addControl(itemBackground);

		int listHeight = 0;
		if (buttonMenu == null) {
			listHeight = showFrame.getShowHeight() - 20
					- img_itemButton.getHeight();
		} else {
			listHeight = showFrame.getShowHeight() - 30
					- img_itemButton.getHeight() * 2;
		}
		DWListbox itemList = new DWListbox(showFrame.getShowWidth() - 20,
				listHeight - 10);
		itemList.setShowBackgroundRect(false);
		itemList.setNearAnchor(showFrame, Tools.HCENTER_TOP, Tools.HCENTER_TOP,
				0, 10);
		showFrame.addControl(itemList);

		DWListSubItem itemSubItem = new DWListSubItem();
		UIWindows.getInstance().m_bag.getItemWindow(itemSubItem, showWidth,
				cell.m_item);
		itemList.addSubItem(itemSubItem);

		// 添加移动按钮

		if (cell.m_item.m_bindingState == ItemConstants.绑定状态_否
				&& cell.m_item.isOperate(ItemConstants.可交易)) {
			DWButton itemButton = new DWButton("添加", img_button);
			if (cell.m_column_type == Cell.TYPE_COLUMN_SHOP) {
				itemButton.setName("移除");
			}
			itemButton.setDownImage(img_anxia);
			itemButton.setNearAnchor(itemList, Tools.HCENTER_TOP,
					Tools.RIGHT_BOTTOM, -showWidth / 2, buttonMenu == null ? 10
							: img_itemButton.getHeight() + 15);
			itemButton.addListener(new DWListener() {
				public void OnDoubleClick() {
				}

				public void OnClick() {
					try {
						DWControlsManager.getInstance().removeControl(showFrame);
						showFrame = null;
						// 移动
						int endIndex = 0;
						if (cell.m_column_type == Cell.TYPE_COLUMN_ROLEBAG) {
							endIndex = 1;
						}

						Tools.debugPrintln("endIndex:" + endIndex
								+ m_column[endIndex]);

						Cell endCell = getExchangCell(m_column[endIndex]);

						if (endCell == null) {
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B,
									"摊位已满，请清理后再进行操作");
						} else if (SELL_TYPE) {
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B,
									"请收摊后再进行移动操作！");
						} else {
							actionMove(cell, endCell);
						}
					} catch (Exception e) {
						// TODO: handle exception
						Tools.debug(e);
					}
					
				}
			});
			showFrame.addControl(itemButton);
		}

		DWControlsManager.getInstance().addControl(showFrame);
	}

	/**
	 * 刷新界面数据
	 * 
	 * @param type
	 */
	public void updateWindow(byte type) {
		if (m_warehose == null
				|| (!DWControlsManager.getInstance().contains(m_warehose)))
			return;

		int updateIndex = -1;
		switch (type) {
		case Cell.TYPE_COLUMN_ROLEBAG:
			updateIndex = 0;
			break;
		case Cell.TYPE_COLUMN_SHOP:
			updateIndex = 1;
			break;
		}

		if (updateIndex > -1) {
			initDate();
			setGrids(updateIndex, m_column[updateIndex]);
		}
	}

	/**
	 * 获取第一个空的格子
	 * 
	 * @param cellArray
	 *            格子数组
	 * @return
	 */
	private Cell getExchangCell(Cell[] cellArray) {
		Cell getCell = null;

		if (cellArray != null) {
			for (Cell cell : cellArray) {
				if (cell.m_item == null) {
					getCell = cell;
					break;
				}
			}
		}

		return getCell;
	}

	/**
	 * 移动
	 * 
	 * @param item
	 */
	private void actionMove(Cell fromCell, Cell toCell) {
		Tools.debugPrintln("移动道具 -> actionMove");

		if (fromCell.m_column_type == Cell.TYPE_COLUMN_SHOP) {
			fromCell.m_item = null;
		} else {

			for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
				if (DWGameManager.getInstance().m_role.m_column_shop[i].m_item != null) {
					if (DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_guid == fromCell.m_item.m_guid) {
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B,
								"您的摆摊栏当中已有该道具，请移除该道具后再进行添加");
						return;
					}

				}
			}

			drawmove(fromCell);
		}

		DWGameManager
				.getInstance()
				.getSendMessage()
				.sendExchangeCellCommand((byte) fromCell.m_column_type,
						fromCell.m_index, (byte) toCell.m_column_type,
						toCell.m_index);
	}

	/**
	 * 扩容
	 */
	private void actionKuorong(byte type, int index) {
		Tools.debugPrintln("扩容道具 -> actionKuorong");
		DWGameManager
				.getInstance()
				.getSendMessage()
				.sendOperaterCellMenuCommand(GameProtocol.操作菜单_扩充栏, type, index);
	}

	public void recvOpenWarehouse(DataInputStream dis) {
		try {
			byte state = dis.readByte();
			if (state == 1) {
				DWControlsManager.getInstance().removeOtherControl();
				open((byte) 0);
			} else {
				DWGameManager.getInstance().addSystemInfo(
						DWGameManager.SYSTEM_INFO_TYPE_B, "仓库开启失败");
			}
		} catch (Exception e) {
			Tools.debug(e);
		}
	}

	private boolean m_nameinit = true;

	public void open(byte index) {
		// TODO Auto-generated method stub
		switch (index) {
		case 0:
			if (m_nameinit) {
				m_name = DWGameManager.getInstance().m_role.m_name + "的摊位";
				m_nameinit = false;
			}
			initDate();
			initFrame();
			DWControlsManager.getInstance().addControl(m_warehose);
			break;
		case 1:
			initDate();
			initFrame();
			DWControlsManager.getInstance().addControl(m_warehose);
			break;
		default:
			break;
		}
	}

	public void drawSetmoney() {

	}

	public void close(byte index) {

		DWControlsManager.getInstance().removeControl(m_warehose);

	}

	public void dispose() {
		// TODO Auto-generated method stub

	}

	public void logic() {
		// TODO Auto-generated method stub

	}

	private DWFrame m_Move_Frame;
	private DWInputBox m_awardMoney;
	private DWInputBox m_awardNum;

	// 贩卖前填写的信息
	public void drawmove(Cell cell) {
		if (cell == null) {
			return;
		}
		final Cell m_cell = cell;
		final SellItem sellitem = new SellItem();

		sellitem.m_UntiPrice = 0;
		sellitem.m_num = 1;
		sellitem.m_name = m_cell.m_item.m_name;
		sellitem.m_guid = m_cell.m_item.m_guid;

		m_Move_Frame = new DWFrame(DWFrame.FRAME_TYPE_HALF_HCENTER, true);
		m_Move_Frame.setClickClose(false);

		// 背景
		DWBackground m_background = new DWBackground(
				m_Move_Frame.getShowWidth() - 20,
				m_Move_Frame.getShowHeight() - 20);
		m_background.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP,
				Tools.LEFT_TOP, 10, 10);
		m_Move_Frame.addControl(m_background);

		// 标题
		DWTitle m_title = new DWTitle("出售", m_Move_Frame);
		m_Move_Frame.addControl(m_title);

		// 物品
		DWGrid m_selectGrid = new DWGrid(img_cell);
		m_selectGrid.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP,
				Tools.LEFT_TOP, 20, 40);
		m_selectGrid.m_data = cell.m_item;
		m_Move_Frame.addControl(m_selectGrid);

		// 道具名称
		DWTextbox tb = new DWTextbox(cell.m_item.m_name);
		tb.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 120, 60);

		m_Move_Frame.addControl(tb);

		// 总价
		final DWTextbox zj = new DWTextbox("总价：" + sellitem.m_UntiPrice
				* sellitem.m_num);

		zj.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 20,
				m_Move_Frame.getShowHeight() - 100);

		m_Move_Frame.addControl(zj);

		// 说明
		final DWTextbox sm = new DWTextbox("说明：交易成功时将收取5%手续费");

		sm.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 20,
				m_Move_Frame.getShowHeight() - 150);

		m_Move_Frame.addControl(sm);

		// 单价
		DWLabel m_msgLabel = new DWLabel("单价：");
		m_msgLabel.setBackgroundColor(Tools.ALPHA);
		m_msgLabel.setNearAnchor(m_selectGrid, Tools.LEFT_TOP, Tools.LEFT_TOP,
				0, m_selectGrid.getShowHeight()+15);
		m_Move_Frame.addControl(m_msgLabel);
		
		int h=0;
		if(DWGameManager.AdpaterType == DWGameManager.AD_HIGH){
			h=40;
		}else{
			h=30;
		}
		m_awardMoney = new DWInputBox(160, h, m_Move_Frame);
		m_awardMoney.setText("" + sellitem.m_UntiPrice);
		m_awardMoney.setNearAnchor(m_msgLabel, Tools.LEFT_TOP, Tools.LEFT_TOP,
				m_msgLabel.getShowWidth(), m_msgLabel.getShowHeight()
						- m_awardMoney.getShowHeight() >> 1);
		m_awardMoney.addListener(new DWListener() {
			public void OnDoubleClick() {

			}

			public void OnClick() {
				
				try {
					if (UINpcShopWindow.isNumber(m_awardMoney.getText())) {
						int num = Integer.valueOf(m_awardMoney.getText());
						if (num < 0) {
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B, "请输入正确的数字！");
						} else if (num == 0) {
							DWGameManager.getInstance().addSystemInfo(
									DWGameManager.SYSTEM_INFO_TYPE_B, "出售价格不得为0元！");
						} else {
							if (num >= 1000000000) {
								num = 999999999;
							}
							sellitem.m_UntiPrice = num;
						}

					}
					m_awardMoney.setText("" + sellitem.m_UntiPrice);

					zj.setText("总价：" + sellitem.m_UntiPrice * sellitem.m_num);
				} catch (Exception e) {
					// TODO: handle exception
					Tools.debug(e);
				}
				

			}
		});
		m_Move_Frame.addControl(m_awardMoney);

		if (!(cell.m_item instanceof ItemEquipment)) {
			sellitem.m_num = cell.m_item.m_num;

			DWTextbox tbs = new DWTextbox("数量：" + sellitem.m_num);
			tbs.setNearAnchor(m_msgLabel, Tools.LEFT_TOP, Tools.LEFT_TOP, 0,
					m_msgLabel.getShowHeight() + 30);
			m_Move_Frame.addControl(tbs);

			// 数量
			// DWLabel m_msgLabel1 = new DWLabel("数量：");
			// m_msgLabel1.setBackgroundColor(Tools.ALPHA);
			// m_msgLabel1.setNearAnchor(m_msgLabel, Tools.LEFT_TOP,
			// Tools.LEFT_TOP, 0, m_msgLabel.getShowHeight() + 30);
			// m_Move_Frame.addControl(m_msgLabel1);
			//
			// m_awardNum = new DWInputBox(160, 30, m_Move_Frame);
			// m_awardNum.setText("" + sellitem.m_num);
			// m_awardNum
			// .setNearAnchor(
			// m_msgLabel1,
			// Tools.LEFT_TOP,
			// Tools.LEFT_TOP,
			// m_msgLabel.getShowWidth(),
			// m_msgLabel.getShowHeight()
			// - m_awardMoney.getShowHeight() >> 1);
			// m_Move_Frame.addControl(m_awardNum);
			// m_awardNum.addListener(new DWListener() {
			// public void OnDoubleClick() {
			//
			// }
			//
			// public void OnClick() {
			// if (UINpcShopWindow.isNumber(m_awardNum.getText())) {
			// int num = Integer.valueOf(m_awardNum.getText());
			// if (num < 0) {
			// DWGameManager.getInstance().addSystemInfo(
			// DWGameManager.SYSTEM_INFO_TYPE_B,
			// "请输入正确的数字！");
			// } else if (num == 0) {
			// DWGameManager.getInstance().addSystemInfo(
			// DWGameManager.SYSTEM_INFO_TYPE_B,
			// "您输入的个数过低！");
			// } else if (num > m_cell.m_item.m_num) {
			// DWGameManager.getInstance().addSystemInfo(
			// DWGameManager.SYSTEM_INFO_TYPE_B,
			// "您输入的数目，大于当前物品持有数！");
			// } else {
			//
			// sellitem.m_num = num;
			//
			// }
			//
			// }
			// m_awardNum.setText("" + sellitem.m_num);
			//
			// zj.setText("总价：" + sellitem.m_UntiPrice * sellitem.m_num);
			//
			// }
			// });

		}

		// 确定返回
		DWButton m_okButton = new DWButton("确定", img_button);
		m_okButton.setDownImage(img_anxia);
		m_okButton.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP,
				0, m_Move_Frame.getShowHeight() - m_okButton.getShowHeight()
						- 10);
		m_okButton.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				try {
					
					if (sellitem.m_UntiPrice == 0) {
						
					} else {
						for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
							if (DWGameManager.getInstance().m_role.m_column_shop[i].m_item == null) {
								
								DWGameManager.getInstance().m_role.m_column_shop[i].m_item = m_cell.m_item
										.Clone();
								
								DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell = sellitem;
								
								DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_num = sellitem.m_num;
								
								DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell.m_UntiPrice = sellitem.m_UntiPrice;
								
								close((byte) 0);
								open((byte) 1);
								
								DWControlsManager.getInstance().removeControl(
										m_Move_Frame);
								
								return;
								
							}
						}
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "您贩卖格子不够，请扩充格子！");
						DWControlsManager.getInstance().removeControl(m_Move_Frame);
					}
				} catch (Exception e) {
					// TODO: handle exception
					Tools.debug(e);
				}

			}
		});
		m_Move_Frame.addControl(m_okButton);

		DWButton m_noButton = new DWButton("返回", img_button);
		m_noButton.setDownImage(img_anxia);
		m_noButton.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP,
				m_Move_Frame.getShowWidth() - m_noButton.getShowWidth() - 10,
				m_Move_Frame.getShowHeight() - m_okButton.getShowHeight() - 10);
		m_noButton.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				DWControlsManager.getInstance().removeControl(m_Move_Frame);
			}
		});
		m_Move_Frame.addControl(m_noButton);

		DWControlsManager.getInstance().addControl(m_Move_Frame);
	}

	private String m_name;
	private DWFrame Name_Frame;

	// 摆摊的名字
	public void drawtanweiname() {

		Name_Frame = new DWFrame(400, DWControlsManager.CONTROL_HEIGHT / 3);
		Name_Frame.setClickClose(false);

		// 背景
		DWBackground m_background = new DWBackground(Name_Frame.getShowWidth(),
				Name_Frame.getShowHeight());
		m_background.setNearAnchor(Name_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP,
				0, 0);
		Name_Frame.addControl(m_background);

		// 标题
		DWTitle m_titles = new DWTitle("店铺名称", Name_Frame);
		Name_Frame.addControl(m_titles);

		// 输入名字
		DWLabel m_msgLabel = new DWLabel("请输入名字：");
		m_msgLabel.setBackgroundColor(Tools.ALPHA);
		m_msgLabel
				.setNearAnchor(
						Name_Frame,
						Tools.LEFT_TOP,
						Tools.LEFT_TOP,
						Name_Frame.getShowWidth()
								- (m_msgLabel.getShowWidth() + 220) >> 1,
						Name_Frame.getShowHeight() - m_msgLabel.getShowHeight() >> 1);
		Name_Frame.addControl(m_msgLabel);

		m_awardMoney = new DWInputBox(220, 30, Name_Frame);
		m_awardMoney.setText(m_name);
		m_awardMoney.setNearAnchor(m_msgLabel, Tools.LEFT_TOP, Tools.LEFT_TOP,
				m_msgLabel.getShowWidth(), m_msgLabel.getShowHeight()
						- m_awardMoney.getShowHeight() >> 1);
		m_awardMoney.addListener(new DWListener() {
			public void OnDoubleClick() {

			}

			public void OnClick() {
				try {
					String str = m_awardMoney.getText();
					
					if (str.length() < 1) {
						m_awardMoney.setText(m_name);
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "您输入的名字过短");
					} else if (str.length() > 10) {
						m_awardMoney.setText(m_name);
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "您输入的名字过长");
					} else {
						
						m_name = str;
						m_awardMoney.setText(m_name);
					}
					
				} catch (Exception e) {
					// TODO: handle exception
					Tools.debug(e);
				}
			}
		});
		Name_Frame.addControl(m_awardMoney);

		// 确定返回
		DWButton m_okButton = new DWButton("确定", img_button);
		m_okButton.setDownImage(img_anxia);
		m_okButton.setNearAnchor(Name_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 0,
				Name_Frame.getShowHeight() - m_okButton.getShowHeight() - 10);
		m_okButton.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				try {
					
					Cell[] cells = DWGameManager.getInstance().m_role
							.getCells(Cell.TYPE_COLUMN_EQUIP);
					Item item = null;
					if (cells != null && ItemConstants.装备_坐骑 < cells.length
							&& cells[ItemConstants.装备_坐骑] != null) {
						item = cells[ItemConstants.装备_坐骑].m_item;
					}
					if (item != null) {
						DWGameManager.getInstance().getSendMessage()
						.sendRideStart(Role.RIDE_DOWN, item.m_guid);
						
					}
					sendBaitanFasong();
				} catch (Exception e) {
					// TODO: handle exception
					Tools.debug(e);
				}
				// 向服务器发送提交请求

			}
		});
		Name_Frame.addControl(m_okButton);

		DWButton m_noButton = new DWButton("返回", img_button);
		m_noButton.setDownImage(img_anxia);
		m_noButton.setNearAnchor(Name_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP,
				Name_Frame.getShowWidth() - m_noButton.getShowWidth() - 10,
				Name_Frame.getShowHeight() - m_okButton.getShowHeight() - 10);
		m_noButton.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				DWControlsManager.getInstance().removeControl(Name_Frame);
			}
		});
		Name_Frame.addControl(m_noButton);

		DWControlsManager.getInstance().addControl(Name_Frame);
	}

	// /////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
	/**
	 * 查看摆摊
	 */
	private DWFrame m_warebuy;
	private DWBackground m_backgrounds;
	private DWTextbox tbss;

	public void drawbaitan() {
		if (m_warebuy != null
				&& DWControlsManager.getInstance().contains(m_warebuy)) {
			DWControlsManager.getInstance().removeControl(m_warebuy);
			m_warebuy = null;
			// 刷新界面
		}
		m_warebuy = new DWFrame(DWFrame.FRAME_TYPE_HALF_LEFT);
		m_warebuy.setThrough(false);
		m_warebuy.setClickClose(false);
		// 绘制背景
		m_backgrounds = new DWBackground(m_warebuy.getShowWidth(),
				m_warebuy.getShowHeight());
		m_backgrounds.setNearAnchor(m_warebuy, Tools.LEFT_TOP, Tools.LEFT_TOP,
				0, 0);
		m_warebuy.addControl(m_backgrounds);

		// 标题
		DWTitle m_title = new DWTitle(m_name, m_warebuy);
		m_warebuy.addControl(m_title);

		// 叉子按钮
		DWImageBox m_close = new DWImageBox(img_close);
		m_close.setNearAnchor(m_title, Tools.RIGHT_VCENTER, Tools.RIGHT_VCENTER);
		m_close.setTouchZoomIn(UIFixWindow.TOUCHZOOM, UIFixWindow.TOUCHZOOM);
		m_close.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				DWControlsManager.getInstance().removeControl(m_warebuy);
			}
		});
		m_warebuy.addControl(m_close);

		final DWGrids m_grids = new DWGrids(img_cell, 10, 5, 4);
		m_grids.setNearAnchor(m_warebuy, Tools.HCENTER_VCENTER,
				Tools.HCENTER_VCENTER);

		for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
			if (DWGameManager.getInstance().m_role.m_column_shop[i].m_item != null) {
				m_grids.m_grids.get(i).m_data = DWGameManager.getInstance().m_role.m_column_shop[i];
			}
		}
		m_grids.m_grids.get(0).setSelect(true);
		addGridListenerbuy(m_grids, 1);
		m_warebuy.addControl(m_grids);

		// 显示出售价格

		tbss = new DWTextbox("<mark c=FFC004>" + "单价：" + "0" + "</mark>");
		tbss.setNearAnchor(m_backgrounds, Tools.LEFT_TOP, Tools.LEFT_TOP, 0,
				m_backgrounds.getShowHeight() - tbss.getShowHeight() - 60);

		DWGrid grid = m_grids.getTouchGrid();
		if (grid.m_data != null) {
			Cell ce = (Cell) grid.m_data;
			tbss.setText("<mark c=FFC004>" + "单价："
					+ (ce.m_item.m_sell.m_UntiPrice) + "</mark>");
		}

		m_warebuy.addControl(tbss);

		// 出售按钮
		DWButton m_okButton = new DWButton("购买", img_button);
		m_okButton.setDownImage(img_anxia);
		m_okButton.setNearAnchor(m_backgrounds, Tools.LEFT_TOP, Tools.LEFT_TOP,
				0, m_warebuy.getShowHeight() - m_okButton.getShowHeight() - 10);
		m_okButton.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {

				DWGrid grid = m_grids.getTouchGrid();

				if (grid.m_data != null) {
					DWControlsManager.getInstance().removeControl(m_Move_Frame);
					showbuy((Cell) grid.m_data);

				} else {

					DWGameManager.getInstance().addSystemInfo(
							DWGameManager.SYSTEM_INFO_TYPE_B, "请选择正确的道具!");
				}
			}
		});
		m_warebuy.addControl(m_okButton);

		DWControlsManager.getInstance().addControl(m_warebuy);

	}

	/**
	 * 购买时显示物品详情及功能
	 */

	private void showItemMessagebuy(final Cell cell, final String[] buttonMenu) {
		Tools.debugPrintln("进入查看");
		if (cell == null || cell.m_item == null) {
			m_selljiage = false;
			return;
		}

		if (showFrame != null
				&& DWControlsManager.getInstance().contains(showFrame)) {
			DWControlsManager.getInstance().removeControl(showFrame);
			showFrame = null;
		}

		if (cell.m_column_type == Cell.TYPE_COLUMN_SHOP) {
			m_selljiage = true;
		} else {
			m_selljiage = false;
		}

		Cell m_bag_cell_equip = DWGameManager.getInstance().m_role.m_column_equip[cell.m_item.m_subtype];
		int showWidth = ((DWGameManager.Screen_Width * 5) >> 4) + 50;

		showFrame = new DWFrame(DWFrame.FRAME_TYPE_HALF_RIGHT);
		showFrame.setThrough(false);
		showFrame.setTouchZoomIn(0, 0);
		showFrame.setClickClose(true);
		showFrame.setBackgroundColor(Tools.ALPHA);

		DWBackground itemBackground = new DWBackground(img_background,
				showFrame.getShowWidth(), showFrame.getShowHeight());
		itemBackground.setNearAnchor(showFrame, Tools.HCENTER_VCENTER,
				Tools.HCENTER_VCENTER);
		showFrame.addControl(itemBackground);

		int listHeight = 0;
		if (buttonMenu == null) {
			listHeight = showFrame.getShowHeight() - 20
					- img_itemButton.getHeight();
		} else {
			listHeight = showFrame.getShowHeight() - 30
					- img_itemButton.getHeight() * 2;
		}
		DWListbox itemList = new DWListbox(showFrame.getShowWidth() - 20,
				listHeight - 10);
		itemList.setShowBackgroundRect(false);
		itemList.setNearAnchor(showFrame, Tools.HCENTER_TOP, Tools.HCENTER_TOP,
				0, 10);
		showFrame.addControl(itemList);

		DWListSubItem itemSubItem = new DWListSubItem();
		UIWindows.getInstance().m_bag.getItemWindow(itemSubItem, showWidth,
				cell.m_item);
		itemList.addSubItem(itemSubItem);

		DWControlsManager.getInstance().addControl(showFrame);
	}

	/**
	 * 购买时格子的监听
	 */
	private void addGridListenerbuy(final DWGrids grids, final int type) {
		grids.addListener(new DWListener() {

			public void OnDoubleClick() {
			}

			public void OnClick() {
				try {
					
					DWGrid flaggrid = grids.getTouchGrid();

					if (selectControl != null && (!flaggrid.equals(selectControl))) {
						selectControl.setSelect(false);
					}
					selectControl = flaggrid;

					DWGrid grid = grids.getTouchGrid();
					if (grid.m_data != null) {
						if (type == 0) {
							if (((Cell) grid.m_data).m_item != null) {

								Cell cell = (Cell) grid.m_data;
								// showItemMessagebuy((Cell) grid.m_data, null);
								DWGameManager
										.getInstance()
										.getSendMessage()
										.sendSeeItemInfos(
												cell.m_item.m_sell.m_guid, null);
							}
						} else {
							Cell cell = (Cell) grid.m_data;
							DWGameManager.getInstance().getSendMessage()
									.sendSeeItemInfos(cell.m_item.m_guid, null);
						}
						Cell ce = (Cell) grid.m_data;
						tbss.setText("<mark c=FFC004>" + "单价："
								+ (ce.m_item.m_sell.m_UntiPrice) + "</mark>");
					} else {
						tbss.setText("<mark c=FFC004>" + "单价：" + "0" + "</mark>");
					}
				} catch (Exception e) {
					// TODO: handle exception
					Tools.debug(e);
				}
				
			}
		});
	}

	// 购买时点击格子后的玩家出售信息

	private void showbuy(final Cell cell) {
		if (cell == null) {
			return;
		}
		final Cell m_cell = cell;
		final SellItem sellitem = new SellItem();

		sellitem.m_UntiPrice = 0;
		sellitem.m_num = 1;
		sellitem.m_name = m_cell.m_item.m_name;
		sellitem.m_guid = m_cell.m_item.m_guid;

		m_Move_Frame = new DWFrame(DWFrame.FRAME_TYPE_HALF_RIGHT, true);
		m_Move_Frame.setClickClose(false);

		// 背景
		DWBackground m_background = new DWBackground(
				m_Move_Frame.getShowWidth() - 20,
				m_Move_Frame.getShowHeight() - 20);
		m_background.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP,
				Tools.LEFT_TOP, 10, 10);
		m_Move_Frame.addControl(m_background);

		// 标题
		DWTitle m_title = new DWTitle("出售", m_Move_Frame);
		m_Move_Frame.addControl(m_title);

		// 物品
		final DWGrid m_selectGrid = new DWGrid(img_cell);
		m_selectGrid.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP,
				Tools.LEFT_TOP, 20, 40);
		m_selectGrid.m_data = cell.m_item;

		m_selectGrid.addListener(new DWListener() {

			public void OnDoubleClick() {
				// TODO Auto-generated method stub

			}

			public void OnClick() {
				// TODO Auto-generated method stub
				// showItemMessagebuy(cell, null);
				if(cell.m_item!=null){
					DWGameManager.getInstance().getSendMessage()
							.sendSeeItemInfos(cell.m_item.m_guid, null);
				}
			}
		});

		m_Move_Frame.addControl(m_selectGrid);

		// 道具名称
		DWTextbox tb = new DWTextbox(cell.m_item.m_name);
		tb.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP, 120, 60);

		m_Move_Frame.addControl(tb);

		// 单价
		DWTextbox tbmoney = new DWTextbox("出售单价："
				+ cell.m_item.m_sell.m_UntiPrice);
		tbmoney.setNearAnchor(m_selectGrid, Tools.LEFT_TOP, Tools.LEFT_TOP, 0,
				m_selectGrid.getShowHeight()+15);
		m_Move_Frame.addControl(tbmoney);

		// 数量
		DWTextbox tbnum = new DWTextbox("出售数量：" + cell.m_item.m_sell.m_num);
		tbnum.setNearAnchor(tbmoney, Tools.LEFT_TOP, Tools.LEFT_TOP, 0,
				tbmoney.getShowHeight()+5);
		m_Move_Frame.addControl(tbnum);

		long money = (long) (cell.m_item.m_sell.m_UntiPrice * cell.m_item.m_sell.m_num);
		// 总价
		DWTextbox tbstr = new DWTextbox("总价：" + money);
		tbstr.setNearAnchor(tbnum, Tools.LEFT_TOP, Tools.LEFT_TOP, 0,
				tbnum.getShowHeight()+5);
		m_Move_Frame.addControl(tbstr);

		// 确定返回
		DWButton m_okButton = new DWButton("购买", img_button);
		m_okButton.setDownImage(img_anxia);
		m_okButton.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP,
				0, m_Move_Frame.getShowHeight() - m_okButton.getShowHeight()
						- 10);
		m_okButton.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				try {
					
					if (DWGameManager.getInstance().m_role.m_gold >= cell.m_item.m_sell.m_TotalPrice) {
						sendBaitanbuy(m_lsid, cell.m_item.m_guid,
								(short) cell.m_item.m_sell.m_num);
					} else {
						
						DWGameManager.getInstance().addSystemInfo(
								DWGameManager.SYSTEM_INFO_TYPE_B, "购买失败，您的金钱不足！");
					}
				} catch (Exception e) {
					// TODO: handle exception
					Tools.debug(e);
				}
			}
		});
		m_Move_Frame.addControl(m_okButton);

		DWButton m_noButton = new DWButton("返回", img_button);
		m_noButton.setDownImage(img_anxia);
		m_noButton.setNearAnchor(m_Move_Frame, Tools.LEFT_TOP, Tools.LEFT_TOP,
				m_Move_Frame.getShowWidth() - m_noButton.getShowWidth() - 10,
				m_Move_Frame.getShowHeight() - m_okButton.getShowHeight() - 10);
		m_noButton.addListener(new DWListener() {
			public void OnDoubleClick() {
			}

			public void OnClick() {
				DWControlsManager.getInstance().removeControl(m_Move_Frame);
			}
		});
		m_Move_Frame.addControl(m_noButton);

		DWControlsManager.getInstance().addControl(m_Move_Frame);
	}

	// 摆摊请求发送
	public void sendBaitanResult() {
		UIWindows.getInstance().m_baitan.SELL_TYPE = DWGameManager
				.getInstance().m_role.isSellStatus();

		UIWindows.getInstance().m_baitan.testAnnouncement();

		m_name = DWGameManager.getInstance().m_role.m_name + "的摊位";

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {
			dos.writeByte((byte) 0);
			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_BAITAN_RESULT,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_BAITAN_RESULT + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;
		} catch (IOException e) {
			Tools.debug(e);
		}
	}

	// 摆摊确定时的请求发送
	public void sendBaitanFasong() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {

			dos.writeUTF(m_name);

			byte num = 0;
			for (Cell c : DWGameManager.getInstance().m_role.m_column_shop) {
				if (c.m_item != null) {
					num++;
				}
			}

			dos.writeByte(num);
			try {
				for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
					if (DWGameManager.getInstance().m_role.m_column_shop[i].m_item != null) {
						dos.writeLong(DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell.m_guid);
						dos.writeLong(DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell.m_UntiPrice);
					}
				}
			} catch (Exception e) {
				// TODO: handle exception
				Tools.debug(e);
			}


			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_BAITAN_FASONG,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_BAITAN_FASONG + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;
		} catch (IOException e) {
			Tools.debug(e);
		}
	}

	// 摆摊是否成功
	public void recvBaitanFasong(DataInputStream dis) throws IOException {
		Tools.debugPrintln("进入判断摆摊是否成功" + GameProtocol.SC_BAITAN_FASONG);
		byte state = dis.readByte();
		switch (state) {
		case 0:
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "摆摊失败！");
			break;
		case 1:
			for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
				DWGameManager.getInstance().m_role.m_column_shop[i].m_item = null;
			}

			SELL_TYPE = true;
			BUTTONMSG[1] = "收摊";
			m_title[1].setName(m_name);
			m_button[1].setName(BUTTONMSG[1]);
			DWControlsManager.getInstance().removeControl(Name_Frame);
			DWControlsManager.getInstance().removeControl(m_warehose);

			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "摆摊成功！");

			break;

		default:
			break;
		}
	}

	// 请求收摊
	public void sendBaitansellend() {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {

			dos.writeByte((byte) 0);

			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_BAITAN_STALLEND,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_BAITAN_STALLEND + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 收摊是否成功
	public void recvBaitansellend(DataInputStream dis) throws IOException {
		Tools.debugPrintln("进入判断收摊是否成功" + GameProtocol.SC_BAITAN_FASONG);
		byte state = dis.readByte();
		switch (state) {
		case 0:
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "收摊失败！");
			break;
		case 1:
			for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
				DWGameManager.getInstance().m_role.m_column_shop[i].m_item = null;
			}

			SELL_TYPE = false;
			BUTTONMSG[1] = "摆摊";
			m_button[1].setName(BUTTONMSG[1]);

			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "收摊成功！");

			open((byte) 0);
			break;

		default:
			break;
		}
	}

	// 请求查看
	public void sendBaitanChakan(long m_id) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {

			dos.writeLong(m_id);

			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_BAITAN_CHAKAN,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_BAITAN_CHAKAN + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 确认查看
	public void recvBaitanChakan(DataInputStream dis) throws IOException {
		Tools.debugPrintln("接收指令：" + GameProtocol.SC_BAITAN_CHAKAN);

		byte state = dis.readByte();

		Tools.debugPrintln("打开状态：" + state);

		if (state == 0) {
			return;
		}

		long id = dis.readLong();

		m_lsid = id;
		m_name = dis.readUTF();

		Role role = DWGameManager.getInstance().m_role;
		boolean flag = id == role.m_gameId ? true : false;

		for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
			 DWGameManager.getInstance().m_role.m_column_shop[i].m_item = null;
		}
		byte size = dis.readByte();
		for (int i = 0; i < size; i++) {
			long itemid = dis.readLong();
			short itemIcon = dis.readShort();
			short itemNum = dis.readShort();
			int Quality = dis.readByte();
			long Unitprice = dis.readLong();
			if (flag) {
				Item item = role.getItemByGuidForAllColumn(itemid);
				if (item != null) {
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item = item
							.Clone();
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell = new SellItem();
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell.m_guid = itemid;
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_num = itemNum;
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell.m_num = itemNum;
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell.m_UntiPrice = Unitprice;
					DWGameManager.getInstance().m_role.m_column_shop[i].m_item.m_sell.m_name = item.m_name;
				}
			} else {
				Item item = new Item();
				item.m_guid = itemid;
				item.m_iconId = itemIcon;
				item.m_num = itemNum;
				item.m_quality = Quality;
				item.m_icon = IconManger.getInstance().getIcon(itemIcon);
				item.m_pinzhi = IconManger.getInstance().getPinzhi(Quality);
				SellItem sell = new SellItem();
				sell.m_guid = itemid;
				sell.m_UntiPrice = Unitprice;
				sell.m_num = itemNum;
				sell.m_TotalPrice = (itemNum * Unitprice);
				item.m_sell = sell;
				DWGameManager.getInstance().m_role.m_column_shop[i].m_item = item;
			}
		}
		if (flag) {
			open((byte) 0);
		} else {
			drawbaitan();
		}
	}

	// 请求购买
	private long m_lsid;

	public void sendBaitanbuy(long m_id, long m_itemid, short num) {
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		DataOutputStream dos = new DataOutputStream(baos);
		try {

			dos.writeLong(m_id);
			dos.writeLong(m_itemid);
			dos.writeShort(num);

			DWGameManager
					.getInstance()
					.getConnection()
					.sendMessage(0, GameProtocol.CS_BAITAN_SELLBUY,
							baos.toByteArray());
			Tools.debugPrintln("发送指令" + GameProtocol.CS_BAITAN_SELLBUY + "!");
			dos.close();
			dos = null;
			baos.close();
			baos = null;

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}

	// 购买是否成功
	public void recvBaitanbuy(DataInputStream dis) throws IOException {
		Tools.debugPrintln("进入判断收摊是否成功" + GameProtocol.SC_BAITAN_FASONG);
		byte state = dis.readByte();
		switch (state) {
		case 0:
			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "购买失败！");
			break;
		case 1:

			DWControlsManager.getInstance().removeControl(m_Move_Frame);

			DWControlsManager.getInstance().removeControl(m_warebuy);

			for (int i = 0; i < DWGameManager.getInstance().m_role.m_column_shop.length; i++) {
				DWGameManager.getInstance().m_role.m_column_shop[i].m_item = null;
			}

			sendBaitanChakan(m_lsid);

			DWGameManager.getInstance().addSystemInfo(
					DWGameManager.SYSTEM_INFO_TYPE_B, "购买成功！");

			break;

		default:
			break;
		}
	}

	public boolean warebuy() {
		return DWControlsManager.getInstance().contains(m_warebuy);
	}

	public boolean m_Move_Frame() {
		return DWControlsManager.getInstance().contains(m_Move_Frame);
	}

}
